home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13656 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.0 KB  |  50 lines

  1. Path: grimsel.zurich.ibm.com!usenet
  2. From: wgk@zurich.ibm.com (Keith Whittingham)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: reversing a string
  5. Date: 9 Apr 1996 15:39:21 GMT
  6. Organization: IBM Research, ZRH
  7. Message-ID: <4ke0b9$rk5@grimsel.zurich.ibm.com>
  8. References: <4k6cjl$j8f@central.server.swt.edu> <4kb1s7$6eu@ibm32.perftech.com> <829058514snz@genesis.demon.co.uk>
  9. Reply-To: wgk@zurich.ibm.com
  10. NNTP-Posting-Host: pine.zurich.ibm.com
  11. X-Newsreader: IBM NewsReader/2 v1.00
  12.  
  13. Ole!  (pronounced Spanishly but written 'olleh')
  14.  
  15. Well here's a starter - no second variable but two recursive functions.
  16. Hopefully that can be reduced to one if my project can spare the time.
  17. (Note to manager: don't worry, just joking).
  18.  
  19. #include <stdio.h>
  20.  
  21. void RecRev2(char *s)
  22.   {
  23.   if(s[1])
  24.     {
  25.     if(s[2])
  26.       {
  27.       RecRev2(&s[1]);
  28.       }
  29.     s[0] ^= s[1]; s[1] ^= s[0]; s[0] ^= s[1];
  30.     }
  31.   }
  32.  
  33. void RecRev1(char *s)
  34.   {
  35.   if(s && *s)
  36.     {
  37.     RecRev2(s);
  38.     RecRev1(&s[1]);
  39.     }
  40.   }
  41.  
  42. int main(int argc, char *argv)
  43.   {
  44.   char *s = "Hello";
  45.   RecRev1(s);
  46.   puts(s);
  47.   return 0;
  48.   }
  49.  
  50.